package com.example.esjd.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author zfj
 * @date 2022/1/13 -11:34
 */
public class HtmlParseUtil {
	public static void main(String[] args) throws IOException {
		//https://search.jd.com/Search?keyword=java
		String url="https://search.jd.com/Search?keyword=java";
		//解析网页
		Document document = Jsoup.parse(new URL(url), 30000);
		//js中的数据都能获取
		Element element = document.getElementById("J_goodsList");
		//获取所有li元素
		assert element != null;
		Elements elements = element.getElementsByTag("li");
		for (Element el : elements) {
			String img = el.getElementsByTag("p-img").eq(0).attr("src");
			String price = el.getElementsByTag("p-price").eq(0).text();
			String title = el.getElementsByTag("p-name").eq(0).text();
			System.out.println("================================================");
			System.out.println(img);
			System.out.println(price);
			System.out.println(title);

		}
	}
}
